package com.zhj.test.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author zhj
 */
public class NIOFileChannel02 {
    public static void main(String[] args) throws IOException {
        File file = new File("E:\\data_file\\log.txt");
        // 创建一个输出流
        FileInputStream fileInputStream = new FileInputStream(file);
        // 通过fileOutputStream 获取对应fileChannel
        // 这个fileChannel 真实类型是 FileChannelImpl
        FileChannel fileChannel = fileInputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate((int)file.length());
        // 将文件 读入缓冲区
        fileChannel.read(byteBuffer);
        // 读写切换
        // byteBuffer.flip();
        System.out.println(new String(byteBuffer.array()));
        fileInputStream.close();
    }
}
